package cn.hpclub.server.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/*
 * DateKit.
 */
public class DateKit{

    private static final String dateFormat = "yyyy-MM-dd";
    // private static String timeFormat = "yyyy-MM-dd HH:mm:ss";

    public static Date toDate(String dateStr, String pattern){
        SimpleDateFormat sdf = null;
        try{
            if(null != pattern && !"".equals(pattern)){
                sdf = new SimpleDateFormat(pattern);
                return sdf.parse(dateStr);
            } else{
                sdf = new SimpleDateFormat(DateKit.dateFormat);
                return sdf.parse(dateStr);
            }
        }
        catch(ParseException e){
            e.printStackTrace();
        }
        return null;
    }

    public static Date toDate(String dateStr){
        return toDate(dateStr, null);
    }

    public static String toStr(Date date){
        return toStr(date, DateKit.dateFormat);
    }

    public static String toStr(Date date, String format){
        SimpleDateFormat sdf = null;
        if(null != format && !"".equals(format)){
            sdf = new SimpleDateFormat(format);
            return sdf.format(date);
        } else{
            sdf = new SimpleDateFormat(DateKit.dateFormat);
            return sdf.format(date);
        }
    }

    private static Date TODAY = new Date();

    public static boolean isToday(Date date){
        if(toStr(date).equals(toStr(TODAY))){// 如果还是今天
            return true;
        } else{
            DateKit.TODAY = new Date();// 比较的基础改为今天，因为new Date()表示当前时间
            return false;
        }
    }

    public static String getLastDay(){
        Calendar calendar = Calendar.getInstance();
        Date date = new Date();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -1);
        date = calendar.getTime();
        SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");
        String lastDay = dateFormater.format(date);
        return lastDay;
    }

    /**
     * @Title: isToday
     * @Description:TODO
     * @param orderIdDate
     * @return boolean
     */
    public static boolean isToday(String date){
        if(date.equals(toStr(new Date(), "yyMMdd"))){
            return true;
        }

        return false;
    }
}
